﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;
using GrayParrot.ServiceBus.Configuration;

namespace GrayParrot.ServiceBus
{
    /// <summary>
    /// 
    /// </summary>
    /// <seealso cref="System.IDisposable" />
    public class Publisher : IDisposable
    {
        public string Endpoint { get; set; }
        public string Topic { get; set; }

        TopicClient m_client = null;

        /// <summary>
        /// Initializes a new instance of the <see cref="Publisher"/> class.
        /// </summary>
        public Publisher()
        {
        }


        public Publisher(Subscription settings)
        {
            Endpoint = settings.ConnectionString;
            Topic = settings.Topic;

            Init();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Publisher"/> class.
        /// </summary>
        /// <param name="endpoint">The endpoint.</param>
        /// <param name="topic">The topic.</param>
        public Publisher(string endpoint, string topic)
        {
            Endpoint = endpoint;
            Topic = topic;

            Init();
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        private void Init()
        {
            m_client = TopicClient.CreateFromConnectionString(Endpoint, Topic);
        }

        /// <summary>
        /// Sends the specified message.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <returns></returns>
        public bool Send(BrokeredMessage message)
        {
            bool retValue = false;

            try
            {
                if (m_client != null)
                {
                    m_client.Send(message);

                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                GrayParrot.Logging.Logger.For<Publisher>().Error("Errore Publisher", ex);

                throw ex;
            }

            return retValue;
        }

        /// <summary>
        /// Sends the specified message with a delay.
        /// </summary>
        /// <param name="message">The message.</param>
        /// <param name="delay">The delay.</param>
        /// <returns></returns>
        public bool Send(BrokeredMessage message, DateTime delay)
        {
            bool retValue = false;

            try
            {
                if (m_client != null)
                {
                    if (delay != DateTime.MinValue)
                    {
                        message.ScheduledEnqueueTimeUtc = delay.ToUniversalTime();
                    }
                    

                    m_client.Send(message);

                    retValue = true;
                }
            }
            catch (Exception ex)
            {
                GrayParrot.Logging.Logger.For<Publisher>().Error("Errore Publisher", ex);

                throw ex;
            }

            return retValue;
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public virtual void Dispose()
        {
            if (m_client != null)
            {
                m_client.Close();
                m_client = null;
            }

            GC.SuppressFinalize(this);
        }
    }
}
